import { connection } from '../app/database/mysql';
import { onAppModel } from './onApp.model';

/**
 * 获取内容
 */
export const getOnApp = async () => {
  // 准备查询
  const statement = `
    SELECT * FROM on_app
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement);

  // 提供数据
  return data;
}

/**
 * 创建内容
 */
export const createOnApp = async (post: onAppModel) => {
  // 准备查询
  const statement = `
    INSERT INTO on_app
    SET ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, post);

  // 提供数据
  return data;
}

/**
 * 更新内容
 */
export const updateOnApp = async (post: onAppModel, postId: number) => {
  // 准备查询
  const statement = `
    UPDATE on_app
    SET ?
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [post, postId]);

  // 提供数据
  return data;
}

/**
 * 删除内容
 */
export const deleteOnApp = async (postId: number) => {
  // 准备查询
  const statement = `
    DELETE FROM on_app
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, postId);

  // 提供数据
  return data;
};
